package com.znsd.r_e_m.reader;


import com.znsd.r_e_m.REMApplication;
import com.znsd.r_e_m.bean.Student;
import com.znsd.r_e_m.dao.EsDataGet;
import com.znsd.r_e_m.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisDataGet {

    public static final Logger log = LoggerFactory.getLogger(REMApplication.class);

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private EsDataGet esDataGet;

    public String get(Integer begin,Integer end){
        List<Student> students = esDataGet.queryAll(begin, end);
        return  "";
    }

    public void refreshRedis(){
        List<Student> students = esDataGet.queryAll();
        for (Student student:students) {
            redisUtils.set(String.valueOf(student.getStuId()),student,1L,TimeUnit.MINUTES);
        }
    }

    public String get(String id,Integer begin,Integer end){
        //查询缓存中是否存在
        boolean hasKey = redisUtils.exists(id);
        String str = "";
        if(hasKey){
            //获取缓存
            Object object =  redisUtils.get(id);
            log.info("从缓存获取的数据"+ object);
            str = object.toString();
        }else{
            //从数es中获取信息
            log.info("从es中获取数据");

            //数据插入缓存（set中的参数含义：key值，user对象，缓存存在时间10（long类型），时间单位）
            redisUtils.set(id,str,10L, TimeUnit.MINUTES);
            log.info("数据插入缓存" + str);
        }
        return str;
    }



}